﻿Public Class ucmysqlconf
    Implements IConfHolder
    Private validationerror As String
    Private mysqlconf As MySqlConfiguration
    Private mysqlh As MySqlHandler
    Private lutil As MySqlUtilities
    Public Function applyConfig() As Boolean Implements IConfHolder.applyConfig
        copysetting()
        If (Not mysqlconf.checkConfig) Then
            validationerrors = mysqlconf.validationerrors
            Return False
        End If
        forcewrite()
        Return True
    End Function

    Public Sub forcewrite() Implements IConfHolder.forcewrite
        copysetting()
        mysqlconf.writeConfig()
    End Sub

    Public Sub loadConfig() Implements IConfHolder.loadConfig
        mysqlconf = New MySqlConfiguration
        mysqlh = MySqlHandler.getInstance
        mysqlutil = New MySqlUtilities
        displayConfig()
    End Sub

    Public Sub revertconfig() Implements IConfHolder.revertconfig
        mysqlconf.mysql_adress = mysqlh.mysqlconf.mysql_adress
        mysqlconf.mysql_database = mysqlh.mysqlconf.mysql_database
        mysqlconf.mysql_password = mysqlh.mysqlconf.mysql_password
        mysqlconf.mysql_port = mysqlh.mysqlconf.mysql_port
        mysqlconf.mysql_user = mysqlh.mysqlconf.mysql_user
        displayConfig()
    End Sub

    Public Property validationerrors As String Implements IConfHolder.validationerrors
        Get
            Return validationerror
        End Get
        Set(ByVal value As String)
            validationerror = value
        End Set
    End Property
    Private Sub displayConfig()
        txtpswd.Text = mysqlconf.mysql_password
        txtdbname.Text = mysqlconf.mysql_database
        txthost.Text = mysqlconf.mysql_adress
        txtport.Text = mysqlconf.mysql_port.ToString
        txtuser.Text = mysqlconf.mysql_user
        chkresetmysql.Checked = mysqlconf.mysql_reset
    End Sub
    Private Sub copysetting()
        mysqlconf.mysql_password = txtpswd.Text
        mysqlconf.mysql_database = txtdbname.Text
        mysqlconf.mysql_adress = txthost.Text
        mysqlconf.mysql_port = commons.toInteger(txtport)
        mysqlconf.mysql_user = txtuser.Text
        mysqlconf.mysql_reset = chkresetmysql.Checked
    End Sub

    Public Property mysqlutil As MySqlUtilities
        Get
            Return lutil
        End Get
        Set(ByVal value As MySqlUtilities)
            lutil = value
        End Set
    End Property


    Private Sub btntest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntest.Click
        Dim msg As String = "MySql Server is Alive and Connectable"
        Dim ic As MsgBoxStyle = vbInformation
        copysetting()
        mysqlutil.setConfig(mysqlconf)
        If (Not mysqlutil.pingMysql) Then
            msg = mysqlutil.MysqlErrorMsg
            ic = MsgBoxStyle.Exclamation
        End If
        simpleMessage(commons.MOD_MYSQL_SETTING, msg, ic)
    End Sub
End Class
